package com.futu.attraction.service;

import com.alibaba.fastjson.JSONObject;
import com.futu.attraction.config.AmapConfig;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

@Service
public class AmapService {
    private final AmapConfig amapConfig;
    // 驾车路线规划 API 的 URL (V5版本)
    private static final String DRIVING_ROUTE_URL = "https://restapi.amap.com/v5/direction/driving";
    // 步行路线规划 API 的 URL (V5版本)
    private static final String WALKING_ROUTE_URL = "https://restapi.amap.com/v5/direction/walking";
    // 公交路线规划 API 的 URL (V5版本)
    private static final String TRANSIT_ROUTE_URL = "https://restapi.amap.com/v5/direction/transit/integrated";

    public AmapService(AmapConfig amapConfig) {
        this.amapConfig = amapConfig;
    }

    /**
     * 获取驾车路线规划
     * @param origin 起点坐标，格式为"经度,纬度"
     * @param destination 终点坐标，格式为"经度,纬度"
     * @return 路线规划结果JSON
     */
    public JSONObject getDrivingRoute(String origin, String destination) throws IOException {
        OkHttpClient client = new OkHttpClient();
        // 构建请求URL，确保获取完整路线信息
        String url = DRIVING_ROUTE_URL + "?origin=" + origin + "&destination=" + destination
                + "&key=" + amapConfig.getKey()
                + "&show_fields=polyline,cost"  // 确保返回路线坐标和花费信息
                + "&extensions=all"  // 获取扩展信息
                + "&strategy=2";  // 策略：2=距离最短

        Request request = new Request.Builder().url(url).build();
        try (Response response = client.newCall(request).execute()) {
            assert response.body() != null;
            String responseBody = response.body().string();
            System.out.println("高德地图驾车路线API响应: " + responseBody);
            return JSONObject.parseObject(responseBody);
        }
    }
    
    /**
     * 获取步行路线规划
     * @param origin 起点坐标，格式为"经度,纬度"
     * @param destination 终点坐标，格式为"经度,纬度"
     * @return 路线规划结果JSON
     */
    public JSONObject getWalkingRoute(String origin, String destination) throws IOException {
        OkHttpClient client = new OkHttpClient();
        String url = WALKING_ROUTE_URL + "?origin=" + origin + "&destination=" + destination
                + "&key=" + amapConfig.getKey()
                + "&show_fields=polyline,cost"
                + "&extensions=all";

        Request request = new Request.Builder().url(url).build();
        try (Response response = client.newCall(request).execute()) {
            assert response.body() != null;
            String responseBody = response.body().string();
            System.out.println("高德地图步行路线API响应: " + responseBody);
            return JSONObject.parseObject(responseBody);
        }
    }
    
    /**
     * 获取公交路线规划
     * @param origin 起点坐标，格式为"经度,纬度"
     * @param destination 终点坐标，格式为"经度,纬度"
     * @param city 城市名称，如"北京"
     * @return 路线规划结果JSON
     */
    public JSONObject getTransitRoute(String origin, String destination, String city) throws IOException {
        OkHttpClient client = new OkHttpClient();
        String encodedCity = URLEncoder.encode(city, StandardCharsets.UTF_8.name());
        String url = TRANSIT_ROUTE_URL + "?origin=" + origin + "&destination=" + destination
                + "&key=" + amapConfig.getKey()
                + "&city=" + encodedCity
                + "&show_fields=polyline,cost"
                + "&extensions=all";

        Request request = new Request.Builder().url(url).build();
        try (Response response = client.newCall(request).execute()) {
            assert response.body() != null;
            String responseBody = response.body().string();
            System.out.println("高德地图公交路线API响应: " + responseBody);
            return JSONObject.parseObject(responseBody);
        }
    }
}